package Project1;

import java.sql.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.tool.hbm2ddl.SchemaExport;


import Project1.HibernateContext;
import Project1.DepartmentEmp;

import Project1.DepartmentEmp;


@Entity
@Table (name = "contact_info")
public class ContactInfo {
	private long emp_no;
	private String email; 

	public ContactInfo() {} 
	public ContactInfo(long emp_no, String email){
		this.emp_no=emp_no;
		this.email=email;
	}
//********************GETTERS SETTERS*********************************************
	
	@Id
	@Column(name="emp_no")
	
	public long getEmpNo(){ return emp_no; }
	public void setEmpNo(long emp_no) { this.emp_no = emp_no; }
	
	@Column(name="email")
	public String getEmail(){ return email; } 
	public void setEmail(String email) { this.email = email; }
	

	public void print(){
		System.out.printf("employee Num: %d email: %s\n", emp_no, email);
	}
	/*
	 * List all Contact Info
	 */
	
	//***************STATIC METHODS**************************
	public static void list(){
		
		Session session = HibernateContext.getSession();
		Criteria criteria = session.createCriteria(ContactInfo.class);
		criteria.addOrder(Order.asc("email"));
		
		List<ContactInfo> con = criteria.list();
		System.out.println("All Contact Info:");
		
		for(ContactInfo c: con){
			c.print();
		}
		session.close();
		
	}
	/*
	 * Gets contact info by employee number and email
	 */
    public static ContactInfo find(long emp_no){
    	// Query using HQL.
        Session session = HibernateContext.getSession();
        Query query = session.createQuery("from ContactInfo where emp_no = :idvar");
        
        query.setLong("idvar", emp_no);
        ContactInfo c = (ContactInfo) query.uniqueResult();
        
        session.close();
        return c;
    }
    
	public static ContactInfo find(String email){
		ContactInfo prototype = new ContactInfo();
		prototype.setEmail(email);
		Example example = Example.create(prototype);
		
		Session session = HibernateContext.getSession();
		Criteria criteria = session.createCriteria(ContactInfo.class);
		criteria.add(example);
		
		ContactInfo con = (ContactInfo) criteria.uniqueResult();
		
		session.close();
		return con;
	}
	
	
} //Contact Info
